﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Monitor
{
    public class MyEventArgs : EventArgs
    {
        private IntPtr _handle;
        public MyEventArgs(IntPtr handle)
        {
            _handle = handle;
        }
        public IntPtr getWindowHandle() { return _handle; }
    }
    
    public partial class MonitorForm : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        GlobalHooks _globalHooks;
        public event EventHandler NewNotepadPPEntry;
        public event EventHandler ClosedNotepadPPEntry;
        List<string> allowedProcesses;
        List<IntPtr> nppHandlers;

        public MonitorForm()
        {
            InitializeComponent();
            _globalHooks = new GlobalHooks(this.Handle);
            allowedProcesses = new List<string>() { "notepad++" };
            nppHandlers = new List<IntPtr>();
            _globalHooks.Shell.Redraw += new GlobalHooks.WindowEventHandler(Shell_WindowCreated);
            //_globalHooks.Shell.WindowActivated += new GlobalHooks.WindowEventHandler(Shell_WindowCreated);
            //_globalHooks.Shell.WindowCreated += new GlobalHooks.WindowEventHandler(Shell_WindowCreated);
            _globalHooks.Shell.WindowDestroyed += new GlobalHooks.WindowEventHandler(Shell_WindowDestroyed);
            //_globalHooks.CBT.Activate += new GlobalHooks.WindowEventHandler(Shell_WindowCreated);
            //_globalHooks.CBT.CreateWindow += new GlobalHooks.WindowEventHandler(Shell_WindowCreated);
            //_globalHooks.CBT.SetFocus += new GlobalHooks.WindowEventHandler(Shell_WindowCreated);
            //_globalHooks.CBT.DestroyWindow += new GlobalHooks.WindowEventHandler(CBT_DestroyWindow);

            //_globalHooks.CBT.Start();
            _globalHooks.Shell.Start();
        }

        void Shell_WindowDestroyed(IntPtr handle)
        {
            if (ClosedNotepadPPEntry != null && nppHandlers.Contains(handle))
            {
                MyEventArgs e = new MyEventArgs(handle);
                ClosedNotepadPPEntry(this, e);
            }
        }

        void Shell_WindowCreated(IntPtr handle)
        {
            uint pid;
            GetWindowThreadProcessId(handle, out pid);
            string pname = Process.GetProcessById((int)pid).ProcessName;

            if (NewNotepadPPEntry != null && allowedProcesses.Contains(pname))
            {
                nppHandlers.Add(handle);
                MyEventArgs e = new MyEventArgs(handle);
                NewNotepadPPEntry(this, e);
            }
        }

        #region WndProc Override
        protected override void WndProc(ref Message m)
        {
            // Check to see if we've received any Windows messages telling us about our hooks
            if (_globalHooks != null)
                _globalHooks.ProcessWindowMessage(ref m);

            base.WndProc(ref m);
        }
        #endregion

    }
}
